package com.yonyou.crm.sact.visitroute.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.enumeration.service.EnumBizServiceImpl;
import com.yonyou.crm.common.field.handler.FieldRelationHandler;
import com.yonyou.crm.common.field.relation.EnumFieldRelation;
import com.yonyou.crm.common.field.relation.FieldRelation;
import com.yonyou.crm.common.field.relation.RefFieldRelation;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.sact.visitroute.entity.VisitrouteNodeVO;
import com.yonyou.crm.sact.visitroute.entity.VisitrouteVO;
import com.yonyou.crm.sys.org.service.SysOrgBizServiceImpl;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class VisitrouteBizServiceImpl extends BaseBizServiceImpl<VisitrouteVO> {
	
	@Autowired
	private SysOrgBizServiceImpl orgService;
	@Autowired
	private EnumBizServiceImpl enumService;
	
	public Page<VisitrouteVO> getList(Page<VisitrouteVO> page,Map<String, Object> paraMap) {
		Page<VisitrouteVO> visitroutePage = null;
		if (paraMap != null) {
			visitroutePage = super.pageSelect(VisitrouteVO.class, page, paraMap.keySet().toArray(new String[0]), paraMap.values().toArray(new Object[0]));
		}
		else {
			visitroutePage = super.pageSelect(VisitrouteVO.class, page, null, null);
		}
		transVisitroute(visitroutePage.getData());
		return visitroutePage;
	}

	public VisitrouteVO save(VisitrouteVO vo) {
		return super.saveVO(vo);
	}
	
	@Override
	protected void beforeSaveVO(VisitrouteVO vo) {
		super.beforeSaveVO(vo);
		Long orgId =  new Long(InvocationInfoProxy.getParameter("orgid"));
		vo.setOrgId(orgId);
	}
	@Override
	protected void afterSaveVO(VisitrouteVO vo) {
		if (vo != null) {
			//保存路线下的网点
			insertVisitrouteNode(vo.getNodeList(), vo.getId());
		}
	}
	
	public VisitrouteVO update(VisitrouteVO vo) {
		return super.updateVO(vo);
	}
	
	@Override
	protected void beforeUpdateVO(VisitrouteVO vo) {
		super.beforeUpdateVO(vo);
	}

	@Override
	protected void afterUpdateVO(VisitrouteVO vo) {
		if (vo != null) {
			//修改拜访路线下网点
			deleteVisitrouteNode(vo.getId());
			insertVisitrouteNode(vo.getNodeList(), vo.getId());
		}
	}
	
	//新增路线下的网点
	private void insertVisitrouteNode(List<VisitrouteNodeVO> vtVOs, Long visitrouteId) {
		if (vtVOs != null && vtVOs.size() > 0) {
			for (VisitrouteNodeVO vtVO : vtVOs) {
				vtVO.setVisitrouteId(visitrouteId);
			}
			dao.saveSql(VisitrouteVO.class, "insertVisitrouteNode", vtVOs);
		}
	}
	//删除路线下的网点
	private void deleteVisitrouteNode(Long visitrouteId) {
		dao.deleteSql(VisitrouteVO.class, "deleteVisitrouteNode", visitrouteId);
	}
	
	public void batchDelete(String[] ids) {
		super.batchDeleteByIDs(VisitrouteVO.class, ids);		
	}
	
	public Object selectFieldsByIds(Object[] ids) {
		Map<String, Object> para = new HashMap<String, Object>();
		para.put("ids", ids);
		return dao.selectList(VisitrouteVO.class, "selectFieldsByIds", para);
	}
	
	private List<VisitrouteVO> transVisitroute(List<VisitrouteVO> list) {
		if (list != null && list.size() > 0) {
			List<FieldRelation<VisitrouteVO>> relations = new ArrayList<FieldRelation<VisitrouteVO>>();
			relations.add(new RefFieldRelation<VisitrouteVO>(VisitrouteVO.class, "orgId", new String[]{"orgName"}, orgService, "selectFieldsByIds"));
			relations.add(new RefFieldRelation<VisitrouteVO>(VisitrouteVO.class, "deptId", new String[]{"deptName"}, orgService, "selectFieldsByIds"));
			relations.add(new EnumFieldRelation<VisitrouteVO>(VisitrouteVO.class, "enableState", "enableStateName", enumService, "enable_state"));
			new FieldRelationHandler<VisitrouteVO>().handler(list, relations);
		}
		return list;
	}
	
}
